﻿package mortal.game.view.autoFight
{
    import fl.data.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.ai.base.*;
    import mortal.game.view.autoFight.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoFightController extends Controller
    {
        private var _module:AutoFightModule;

        public function AutoFightController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new AutoFightModule();
                this._module.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            }
            return this._module;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.SkillAdd, this.updateAllSkillInfos);
            Dispatcher.addEventListener(EventName.AutoFight_ShowHide, this.showHideAutoSetModule);
            Dispatcher.addEventListener(EventName.AutoFight_A, this.autoFightHandler);
            Dispatcher.addEventListener(EventName.AutoFight_Stop, this.stopAutoFightHandler);
            Dispatcher.addEventListener(EventName.AutoFight_BossNotSelect, this.bossNotSelectHandler);
            Dispatcher.addEventListener(EventName.AutoFight_Round, this.roundFightHandler);
            Dispatcher.addEventListener(EventName.AutoFight_LoadDefault, this.loadDefaultHandler);
            Dispatcher.addEventListener(EventName.AutoFight_Save, this.saveHandler);
            Dispatcher.addEventListener(EventName.AutoFight_PlanChange, this.planChangeHandler);
            Dispatcher.addEventListener(EventName.CDPublicCDEnd, this.publicCdEndHandler);
            Dispatcher.addEventListener(EventName.AutoFightAIStatusChange, this.autoFightStatusChangeHandler);
            return;
        }// end function

        private function addToStageHandler(event:Event) : void
        {
            this.updateAllDatas(false);
            return;
        }// end function

        private function autoFightStatusChangeHandler(event:DataEvent) : void
        {
            var _loc_2:* = Boolean(event.data);
            if (this._module && !this._module.isHide)
            {
                this._module.updateAutoFightStatus(_loc_2);
            }
            if (event.data == false)
            {
                GameController.guide.guideUseXp(false);
            }
            return;
        }// end function

        private function showHideAutoSetModule(event:DataEvent) : void
        {
            if (this._module && !this._module.isHide)
            {
                this._module.hide();
                return;
            }
            view.show();
            return;
        }// end function

        private function publicCdEndHandler(event:DataEvent) : void
        {
            return;
        }// end function

        private function updateAllSkillInfos(param1:Object) : void
        {
            if (this._module && !this._module.isHide)
            {
                this.updateAllDatas(false);
            }
            return;
        }// end function

        private function planChangeHandler(event:DataEvent) : void
        {
            if (this._module == null)
            {
                return;
            }
            var _loc_2:* = this._module.getPlanIndex();
            var _loc_3:* = cache.autoFight.getMainSkillData(_loc_2, false, false);
            this._module.updateMainSkillList(_loc_3);
            return;
        }// end function

        private function roundFightHandler(event:DataEvent) : void
        {
            var _loc_2:IAICommand = null;
            if (!cache.autoFight.isCurMapCanAutoFight)
            {
                return;
            }
            if (!Game.isSceneInit())
            {
                return;
            }
            cache.autoFight.resetBossPointIndex();
            if (AIManager.curAI != null && AIManager.curAI is CollectAI && CollectAI.isWorking)
            {
                _loc_2 = AIManager.curAI;
                AIManager.curAI = null;
                AIManager.onAIControl(AIType.AutoFight, GameConst.RoundFightDistance);
                (_loc_2 as AICommand).callback = AutoFightAI.instance.startAIList;
                AutoFightAI.instance.curAI = _loc_2;
            }
            else
            {
                AIManager.onAIControl(AIType.AutoFight, GameConst.RoundFightDistance);
            }
            return;
        }// end function

        private function autoFightHandler(event:DataEvent) : void
        {
            var _loc_2:IAICommand = null;
            if (!cache.autoFight.isCurMapCanAutoFight)
            {
                return;
            }
            if (!Game.isSceneInit())
            {
                return;
            }
            cache.autoFight.resetBossPointIndex();
            if (AIManager.curAI != null && AIManager.curAI is CollectAI && CollectAI.isWorking)
            {
                _loc_2 = AIManager.curAI;
                AIManager.curAI = null;
                AIManager.onAIControl(AIType.AutoFight, -1);
                (_loc_2 as AICommand).callback = AutoFightAI.instance.startAIList;
                AutoFightAI.instance.curAI = _loc_2;
            }
            else
            {
                AIManager.onAIControl(AIType.AutoFight, -1);
            }
            return;
        }// end function

        private function stopAutoFightHandler(event:DataEvent) : void
        {
            AIManager.cancelAll();
            return;
        }// end function

        private function loadDefaultHandler(event:DataEvent) : void
        {
            this.updateAllDatas(true);
            return;
        }// end function

        private function saveHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.autoFight.saveCacheDatas();
            if (this._module != null)
            {
                _loc_2 = this._module.saveModuleDatas() || _loc_2;
            }
            if (_loc_2)
            {
                ClientSetting.save();
            }
            return;
        }// end function

        private function bossNotSelectHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SelectBossData;
            if (_loc_2 == null)
            {
                return;
            }
            cache.autoFight.setBossNotSelect(_loc_2.boss.code, !_loc_2.selected);
            return;
        }// end function

        private function updateAllDatas(param1:Boolean) : void
        {
            var _loc_2:* = cache.autoFight.getBossList(Game.mapInfo.mapId, param1);
            this._module.updateBossList(_loc_2);
            var _loc_3:* = AFSkillPlanIndex.plan1;
            if (ClientSetting.local.getIsDone(IsDoneType.UseSecondSkillPlan))
            {
                _loc_3 = AFSkillPlanIndex.plan2;
            }
            _loc_2 = cache.autoFight.getMainSkillData(_loc_3, param1);
            this._module.updateMainSkillList(_loc_2);
            var _loc_4:* = cache.autoFight.getAssistSkillData(param1);
            this._module.updateAssistSkillList(_loc_4);
            var _loc_5:* = cache.autoFight.getWizardSkills();
            this._module.updateWizardSkillList(_loc_5);
            this._module.updateSelected(param1);
            return;
        }// end function

    }
}
